package mobi.hami.huapin.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.model.NoteInfo;
import mobi.hami.huapin.model.NoteSort;
import mobi.hami.huapin.model.UserInvoice;
import mobi.hami.huapin.model.UserReturnGoodsLog;
import mobi.hami.huapin.service.ManagementService;
import mobi.hami.huapin.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by 子龙 on 2018/9/05.
 */
@RestController
@RequestMapping(value = "/mangment", produces = "application/json")
@Api(description = "供管理后台使用的api接口")
public class ManagementController {

    @Autowired
    private ManagementService managementService;

    private static Integer startPageNum = 1;
    private static Integer pageSize = 10;

    /**
     * 发票列表
     * @param mm
     * @return
     */
    @RequestMapping(value = "/userinvoices.do", method = RequestMethod.POST)
    @ApiOperation(value = "发票列表(发票列表+搜索+分页)", notes = "{\n" +
            "\"startPageNum\":页码(可选参数),\n" +
            "\"pageSize\":每页容量(可选参数,默认10),\n" +
            "\"order_id\":订单id(可选参数)\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "Response Body\n" +
                    "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"data\": {\n" +
                    "    \"startPageNum\": 页码,\n" +
                    "    \"totalNum\": 总记录数,\n" +
                    "    \"pageSize\": 每页容量,\n" +
                    "    \"totalPageNum\": 总页数,\n" +
                    "    \"list\": [\n" +
                    "      {\n" +
                    "        \"id\": 发票id,\n" +
                    "        \"user_id\": 用户id,\n" +
                    "        \"open_id\": \"用户openId\",\n" +
                    "        \"order_id\": 订单id,\n" +
                    "        \"type\": 发票类型0个人/非企业,1企业单位,\n" +
                    "        \"ein\": 税号(企业发票才有税号),\n" +
                    "        \"content\": \"发票内容\",\n" +
                    "        \"head\": \"发票抬头\",\n" +
                    "        \"money\": 发票金额,\n" +
                    "        \"contact_name\": \"联系人称谓\",\n" +
                    "        \"contact_mobile\": \"联系人电话\",\n" +
                    "        \"contact_address\": \"联系人地址\",\n" +
                    "        \"create_time\": \"创建时间\",\n" +
                    "        \"accept_time\": 受理时间,\n" +
                    "        \"complete_time\": 发票完成时间按,\n" +
                    "        \"status\": 发票状态0:申请中1:已同意开发票2:发票完成(发票已经线下邮递,且用户收到)\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}")
    })
    public Result userinvoices(@RequestBody Map<String,Object> mm){
        HashMap<String,Object> data = new HashMap<String,Object>();
        if(null != mm && null != mm.get("startPageNum") && !mm.get("startPageNum").equals(0)){
            startPageNum = new Integer(String.valueOf(mm.get("startPageNum")));
        }
        if(null != mm && null != mm.get("pageSize") && !mm.get("pageSize").equals(0)){
            pageSize = new Integer(String.valueOf(mm.get("pageSize")));
        }
        Page page =PageHelper.startPage(startPageNum, pageSize,true).reasonable(true);
        List<UserInvoice> list = managementService.selectUserInvoiceWitchParam(mm);
        PageInfo<UserInvoice> pi = new PageInfo<>(list);
        int totalRecord = (int)page.getTotal();
        data.put("totalNum",totalRecord);
        int totalPageNum = (totalRecord  +  pageSize  - 1) / pageSize;
        data.put("totalPageNum",totalPageNum);
        data.put("startPageNum",startPageNum);
        data.put("pageSize",pageSize);
        data.put("list",list);
        return Results.successWithData(data);
    }


    /**
     * 同意开发票
     * @return
     */
    @RequestMapping(value = "/agreeduserinvoice.do", method = RequestMethod.POST)
    @ApiOperation(value = "同意开发票", notes = "前提条件该发票申请处于申请状态,即status=0,改为status=1 ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userinvoice_id", value = "发票申请记录id", example = "6", paramType = "query", required = true, dataType = "int"),
            }
    )
    public Result agreeduserinvoice(Integer userinvoice_id){
        return managementService.agreeduserinvoice(userinvoice_id);
    }

    /**
     * 发票已完成操作
     * @return
     */
    @RequestMapping(value = "/completeuserinvoice.do", method = RequestMethod.POST)
    @ApiOperation(value = "发票已完成操作", notes = "前提条件该发票申请处于已同意状态,即status=1,改为status=2 ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userinvoice_id", value = "发票申请记录id", example = "6", paramType = "query", required = true, dataType = "Integer"),
            }
    )
    public Result completeuserinvoice(Integer userinvoice_id){
        return managementService.completeuserinvoice(userinvoice_id);
    }


    /**
     * 退货记录列表
     * @param mm
     * @return
     */
    @RequestMapping(value = "/returngoods.do", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "退货记录列表(列表+搜索+分页)", notes = "{\n" +
            "\"startPageNum\":页码(可选参数),\n" +
            "\"pageSize\":每页容量(可选参数,默认10),\n" +
            "\"order_id\":订单id(可选参数),\n" +
            "\"courier_code\":快递单号(可选参数),\n" +
            "\"contact_name\":退货联系人称谓(可选参数),\n" +
            "\"contact_mobile\":退货联系人电话(可选参数)\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"data\": {\n" +
                    "    \"startPageNum\": 页码,\n" +
                    "    \"totalNum\": 总记录数,\n" +
                    "    \"pageSize\": 每页容量,\n" +
                    "    \"totalPageNum\": 总页数,\n" +
                    "    \"list\": [\n" +
                    "      {\n" +
                    "        \"id\": 16,\n" +
                    "        \"user_id\": 用户id,\n" +
                    "        \"open_id\": \"用户openid\",\n" +
                    "        \"the_order_id\": 订单id,\n" +
                    "        \"courier_agency_id\": 快递机构id,\n" +
                    "        \"courier_code\": 快点单号,\n" +
                    "        \"contact_name\": 联系人称谓,\n" +
                    "        \"contact_mobile\": 联系人电话,\n" +
                    "        \"reason\": \"商品有损坏\",\n" +
                    "        \"imgs\": \"http://oss.hami.mobi/38659779-6261-4eb3-80a1-4e28e7cc8260.jpg,\",\n" +
                    "        \"create_time\": \"创建时间\",\n" +
                    "        \"accept_time\": 受理时间,\n" +
                    "        \"refuse_time\": 拒绝时间,\n" +
                    "        \"complete_time\": 退货完成时间,\n" +
                    "        \"status\": 退货状态0:申请中1:同意退货2:用户已提交快递信息3:拒绝退货4:统确认后退货完成\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}")
    })
    public Result returngoods(@RequestBody  Map<String,Object> mm){

        HashMap<String,Object> data = new HashMap<String,Object>();
        if(null != mm && null != mm.get("startPageNum")){
            startPageNum = new Integer(String.valueOf(mm.get("startPageNum")));
        }
        if(null != mm && null != mm.get("pageSize")){
            pageSize = new Integer(String.valueOf(mm.get("pageSize")));
        }
        Page page = PageHelper.startPage(startPageNum, pageSize,true).reasonable(true);
        List<UserReturnGoodsLog> list = managementService.selectWitchParam(mm);
        PageInfo<UserReturnGoodsLog> pi = new PageInfo<>(list);
        int totalRecord = (int)page.getTotal();
        data.put("totalNum",totalRecord);
        int totalPageNum = (totalRecord  +  pageSize  - 1) / pageSize;
        data.put("totalPageNum",totalPageNum);
        data.put("startPageNum",startPageNum);
        data.put("pageSize",pageSize);
        data.put("list",list);
        return Results.successWithData(data);
    }

    /**
     * 同意退货
     * @return
     */
    @RequestMapping(value = "/agreedreturngoods.do", method = RequestMethod.POST)
    @ApiOperation(value = "同意退货", notes = "前提status=0,即处于申请状态才能同意退货 ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userReturnGoodsLog_id", value = "退货记录id", example = "6", paramType = "query", required = true, dataType = "Integer"),
            }
    )
    public Result agreedreturngoods(Integer userReturnGoodsLog_id){
        return managementService.agreedreturngoods(userReturnGoodsLog_id);
    }

    /**
     * 拒绝退货
     * @return
     */
    @RequestMapping(value = "/refusereturngoods.do", method = RequestMethod.POST)
    @ApiOperation(value = "拒绝退货", notes = "前提status=0,即处于申请状态才能拒绝退货")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userReturnGoodsLog_id", value = "退货记录id", example = "6", paramType = "query", required = true, dataType = "Integer"),
            }
    )
    public Result refusereturngoods(Integer userReturnGoodsLog_id){
        return managementService.refusereturngoods(userReturnGoodsLog_id);
    }

    /**
     * 退货完成
     * @return
     */
    @RequestMapping(value = "/completereturngoods.do", method = RequestMethod.POST)
    @ApiOperation(value = "退货完成", notes = " 前提status=1,即处于同意退货状态才能标志该退货记录为退货完成 ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userReturnGoodsLog_id", value = "退货记录id", example = "6", paramType = "query", required = true, dataType = "Integer"),
            }
    )
    public Result completereturngoods(Integer userReturnGoodsLog_id){
        return managementService.completereturngoods(userReturnGoodsLog_id);
    }


    /**
     * 纸条库管理-添加纸条
     */
    @RequestMapping(value = "/addnote.do", method = RequestMethod.POST)
    @ApiOperation(value = "纸条库管理-添加纸条", notes = "")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "content", value = "纸条内容", example = "皮皮虾", paramType = "query", required = true, dataType = "String"),
            }
    )
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"status\": 0,\n" +
                    "  \"msg\": \"添加成功!\"\n" +
                    "}")
    })
    public Result addNote(String  content){
       return managementService.addNote(content);
    }



    /**
     * 纸条库管理-纸条列表
     * @param
     */
    @RequestMapping(value = "/noteinfos.do", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "纸条库管理-纸条列表", notes = "{\n" +
            "  \"startPageNum\": 页码,\n" +
            "  \"pageSize\":每页容量,\n" +
            "  \"id\":纸条id,\n" +
            "  \"comtent\":纸条内容,\n" +
            "  \"start_used_times\":被使用的次数搜索区间开始位置,\n" +
            "  \"end_used_times\":被使用的次数搜索区间结束位置,\n" +
            "  \"start_thumbup_times\":点赞的次数搜索区间开始位置,\n" +
            "  \"end_thumbup_times\":点赞的次数搜索区间结束位置,\n" +
            "  \"status\":状态0上线1下线,\n" +
            "  \"start_cretate_time\":创建时间搜索区间开始位置,\n" +
            "  \"end_cretate_time\":创建时间搜索区间结束位置\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"data\": {\n" +
                    "    \"startPageNum\": 页码,\n" +
                    "    \"totalNum\": 总记录数,\n" +
                    "    \"pageSize\": 每页容量,\n" +
                    "    \"totalPageNum\": 总页数,\n" +
                    "    \"list\": [\n" +
                    "      {\n" +
                    "        \"id\": 主键,\n" +
                    "        \"comtent\": \"纸条内容\",\n" +
                    "        \"cretate_time\": \"创建时间\",\n" +
                    "        \"creator_id\": 创建者id,\n" +
                    "        \"space_id\": 空间id,\n" +
                    "        \"used_times\": 被使用的次数,\n" +
                    "        \"thumbup_times\": 被点赞的次数,\n" +
                    "        \"note_class_id\": 所属分类id,\n" +
                    "        \"status\": 状态0上线1下线\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}")
    })
    public Result noteinfos(@RequestBody @ApiParam(value ="") Map<String,Object> mm){
        HashMap<String,Object> data = new HashMap<String,Object>();
        if(null != mm && null != mm.get("startPageNum")){
            startPageNum = new Integer(String.valueOf(mm.get("startPageNum")));
        }
        if(null != mm && null != mm.get("pageSize")){
            pageSize = new Integer(String.valueOf(mm.get("pageSize")));
        }
        Page page = PageHelper.startPage(startPageNum, pageSize,true).reasonable(true);
        List<NoteInfo> list = managementService.selectNoteinfosWitchParam(mm);
        PageInfo<NoteInfo> pi = new PageInfo<>(list);
        int totalRecord = (int)page.getTotal();
        data.put("totalNum",totalRecord);
        int totalPageNum = (totalRecord  +  pageSize  - 1) / pageSize;
        data.put("totalPageNum",totalPageNum);
        data.put("startPageNum",startPageNum);
        data.put("pageSize",pageSize);
        data.put("list",list);
        return Results.successWithData(data);
    }


    /**
     * 纸条库管理-添加纸条分类
     */
    @RequestMapping(value = "/addnotesort.do", method = RequestMethod.POST)
    @ApiOperation(value = "纸条库管理-添加纸条分类", notes = "")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "name", value = "纸条分类名称", example = "搞笑", paramType = "query", required = true, dataType = "String"),
            }
    )
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"status\": 0添加成功1添加失败2纸条分类名称为空3该纸条分类名称已经存在\n" +
                    "  \"msg\": \"我是提示消息!\"\n" +
                    "}")
    })
    public Result addNoteSort(String  name){
        return managementService.addNoteSort(name);
    }



    /**
     * 纸条库管理-纸条分类列表
     * @param
     */
    @RequestMapping(value = "/notesorts.do", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "纸条库管理-纸条分类列表", notes = "{\n" +
            "\"startPageNum\":页码(可选参数),\n" +
            "\"pageSize\":每页容量(可选参数),\n" +
            "\"name\":\"分类名称(可选参数)\",\n" +
            "\"create_user_id\":系统创建者id(可选参数),\n" +
            "\"start_create_time\":搜索创建时间区间开始(可选参数),\n" +
            "\"end_create_time\":搜索创建时间区间结束(可选参数),\n" +
            "\"status\":状态, 0表示未用，1表示启用(可选参数)\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"data\": {\n" +
                    "    \"startPageNum\": 页码,\n" +
                    "    \"totalNum\": 总记录数,\n" +
                    "    \"pageSize\": 每页容量,\n" +
                    "    \"totalPageNum\": 总页数,\n" +
                    "    \"list\": [\n" +
                    "      {\n" +
                    "        \"id\": 主键,\n" +
                    "        \"name\": \"分类名称\",\n" +
                    "        \"create_time\": \"创建时间\",\n" +
                    "        \"create_user_id\": 创建者id,\n" +
                    "        \"order_num\": 排序码,\n" +
                    "        \"online_time\": \"上线时间\",\n" +
                    "        \"status\": 状态, 0表示未用，1表示启用,\n" +
                    "        \"version\": 版本号\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }")
    })
    public Result notesorts(@RequestBody @ApiParam(value ="") Map<String,Object> mm){
        HashMap<String,Object> data = new HashMap<String,Object>();
        if(null != mm && null != mm.get("startPageNum")){
            startPageNum = new Integer(String.valueOf(mm.get("startPageNum")));
        }
        if(null != mm && null != mm.get("pageSize")){
            pageSize = new Integer(String.valueOf(mm.get("pageSize")));
        }
        Page page = PageHelper.startPage(startPageNum, pageSize,true).reasonable(true);
        List<NoteSort> list = managementService.selectNoteSortsWitchParam(mm);
        PageInfo<NoteSort> pi = new PageInfo<>(list);
        int totalRecord = (int)page.getTotal();
        data.put("totalNum",totalRecord);
        int totalPageNum = (totalRecord  +  pageSize  - 1) / pageSize;
        data.put("totalPageNum",totalPageNum);
        data.put("startPageNum",startPageNum);
        data.put("pageSize",pageSize);
        data.put("list",list);
        return Results.successWithData(data);
    }


    /**
     * 纸条库管理-纸条分类上下线
     */
    @RequestMapping(value = "/updatanotesortstatus.do", method = RequestMethod.POST)
    @ApiOperation(value = "纸条库管理-纸条分类上下线", notes = "")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "纸条分类id", example = "1", paramType = "query", required = true, dataType = "int"),
                    @ApiImplicitParam(name = "status", value = "纸条分类状态, 0表示未用/下线，1表示启用/上线", example = "1", paramType = "query", required = true, dataType = "int"),
            }
    )
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示信息!\"\n" +
                    "}")
    })
    public Result updataNoteSortStatus(Integer id,Integer status){
        return managementService.updataNoteSortStatus(id,status);
    }



    /**
     * 纸条库管理-纸条分类拍序号交换位置
     */
    @RequestMapping(value = "/updatanotesortoirdernum.do", method = RequestMethod.POST)
    @ApiOperation(value = "纸条库管理-纸条分类拍序号交换位置(上下移动)", notes = "")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "当前纸条分类记录id", example = "1", paramType = "query", required = true, dataType = "int"),
                    @ApiImplicitParam(name = "order_num", value = "当前纸条分类记录的排序号", example = "1", paramType = "query", required = true, dataType = "int"),
                    @ApiImplicitParam(name = "type", value = "0上移,1下移", example = "1", paramType = "query", required = true, dataType = "int"),
            }
    )
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示信息!\"\n" +
                    "}")
    })
    public Result updataNoteSortOrderNum(Integer id,Integer order_num,Integer type){
        return managementService.updataNoteSortOrderNum(id,order_num,type);
    }


}
